home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / networking / ip / ka9q / MNetsrc.hqx / Mac TCP_IP Source v.33 / mac_binsubs.c < prev    next >
Text File  |  1989-03-30  |  7KB  |  331 lines

  1. /*
  2. *
  3. *    MacBinary Subroutines.    
  4. *
  5. */
  6.  
  7. #include "global.h"
  8. #include <FileMgr.h>
  9. #include "MacBinary.h"
  10.  
  11. char *strncpy(), *malloc();
  12.  
  13. #define BLOCKS(x)    ((x+127)/128)
  14. #define lmove(f,t)    movmem(f,t,4)
  15.  
  16. MBFile *MBopen( file, vrefnum, mode)
  17. char *file;
  18. short vrefnum,mode;
  19. {
  20.     MBFile *mbfp;
  21.     int err;
  22.  
  23.     mbfp=(MBFile *)calloc(1,sizeof(MBFile));
  24.     if (mbfp==0L)
  25.         return(0L);
  26.     movmem( file, mbfp->name,strlen(file)+1);
  27.     CtoPstr(mbfp->name);
  28.     mbfp->vrefnum=vrefnum;
  29.     mbfp->mode = mode;
  30.  
  31.     if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
  32.         if ((err==-43) && (mode & MB_WRITE)) {
  33.             Create( mbfp->name, vrefnum, '????','TEXT');
  34.             if (FSOpen( mbfp->name, vrefnum, &mbfp->fd)) 
  35.                 return( 0L);
  36.             }
  37.         else 
  38.             return(0L);
  39.         }
  40.  
  41.     mbfp->binary=0;
  42.     mbfp->sector1=1;
  43.     mbfp->fork=0;
  44.     mbfp->gothdr=0;
  45.     mbfp->hdrcnt=0;
  46.     setmem(&mbfp->headbuf,128,0);
  47.     return( mbfp);
  48. }
  49.  
  50. MBwrite( out, buffer, size)
  51. MBFile *out;
  52. int size;
  53. char *buffer;
  54. {
  55.     int rsize;
  56.     char *hdpt;
  57.     
  58.     rsize = size;
  59.     
  60.     if(out->sector1 && !out->gothdr){
  61.         hdpt = (char *)&out->headbuf + out->hdrcnt;
  62.         if((out->hdrcnt + size) > 128){
  63.             movmem(buffer,hdpt,(128 - out->hdrcnt));
  64.             if(isMacBinary(&out->headbuf)){
  65.                 ProcessMBHead(out,&out->headbuf);
  66.                 buffer += (128 - out->hdrcnt);
  67.                 size -= (128 - out->hdrcnt);
  68.                 bwrite(out,buffer,size);
  69.                 out->gothdr = 1;
  70.                 return(size);
  71.             } else {
  72.                 bwrite(out,&out->headbuf,128);
  73.                 buffer += (128 - out->hdrcnt);
  74.                 size -= (128 - out->hdrcnt);
  75.                 bwrite(out,buffer,size);
  76.                 return(rsize);
  77.             }
  78.         } else {
  79.             movmem(buffer,hdpt,size);
  80.             out->hdrcnt += size;
  81.             return(size);
  82.         }
  83.     }
  84.  
  85.     bwrite( out,buffer,size);
  86.     return(size);
  87. }
  88.  
  89. bwrite( out, buffer, size)
  90. MBFile *out;
  91. int size;
  92. char *buffer;
  93. {
  94.     long len=size;
  95.  
  96.     if (out->binary) {
  97.         if (out->bytes>0) {
  98.             if (out->bytes< len) len = out->bytes;
  99.             FSWrite( out->fd, &len, buffer);
  100.             out->bytes -= len;
  101.             buffer +=len;
  102.             size -= (int)len;
  103.             }
  104.         if (out->bytes<= 0) {
  105.             if (!out->fork) {
  106.                 out->fork=1;
  107.                 out->bytes=BLOCKS(out->rlen)*128;
  108.                 SetEOF( out->fd, (long) out->dlen);
  109.                 FSClose( out->fd);
  110.                 if (out->bytes) {
  111.                     OpenRF( out->name, out->vrefnum,&out->fd);
  112.                     if (size) {
  113.                         len = (long) size;
  114.                         FSWrite( out->fd, &len, buffer);
  115.                         }
  116.                     }
  117.                 else
  118.                     out->fd=0;
  119.                 }
  120.             else SetEOF( out->fd, (long) out->rlen);
  121.             }
  122.         }
  123.     else {
  124.         FSWrite( out->fd, &len, buffer);
  125.         }
  126. }
  127.  
  128. MBclose( out)
  129. MBFile *out;
  130. {
  131.     FileParam finfo;
  132.     long fpos;
  133.  
  134.     if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
  135.         if (out->fork)
  136.             SetEOF( out->fd, (long) out->rlen);
  137.         else
  138.             SetEOF( out->fd, (long) out->dlen);
  139.  
  140.         FSClose( out->fd);
  141.         GetFileInfo( 0, out->name, &finfo);
  142.     
  143.         movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
  144.         lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
  145.         lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
  146.         finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
  147.         finfo.ioFlRLgLen=out->rlen;
  148.         finfo.ioFlLgLen =out->dlen;
  149.     
  150.         SetFileInfo( 0, out->name, &finfo);
  151.         }
  152.     else if (out->mode & MB_WRITE) {
  153.         GetFPos( out->fd, &fpos);
  154.         SetEOF(  out->fd,  fpos);
  155.         FSClose( out->fd);
  156.         }
  157.     else
  158.         FSClose( out->fd);
  159. }
  160.  
  161. ProcessMBHead(out,header)
  162. MBFile *out;
  163. MBHead *header;
  164. {
  165.     int err;
  166.     char *nptr,temp[256];
  167.  
  168.     movmem( header, &out->header, sizeof(MBHead));
  169.     out->binary=1;
  170.     lmove( &header->dflen[0], &out->dlen);
  171.     lmove( &header->rflen[0], &out->rlen);
  172.     out->bytes= BLOCKS(out->dlen)*128;
  173.     out->fork=0;
  174.     out->sector1=0;
  175.  
  176.     FSClose(out->fd);
  177.     if (FSDelete( out->name, out->vrefnum))
  178.         printf("mac_binsubs: Error Deleting Old File.\n");
  179.     PtoCstr(out->name);
  180.     PtoCstr(&out->header.nlen);
  181.     nptr = rindex(out->name,':');
  182.     if(nptr != NULL){
  183.         *++nptr = '\0';
  184.         sprintf(temp,"%s%s",out->name,&out->header.nlen);
  185.     } else {
  186.         sprintf(temp,"%s:%s",out->name,&out->header.nlen);
  187.     }
  188.     movmem(temp,out->name,strlen(temp)+1);
  189.     CtoPstr(out->name);
  190.     CtoPstr(&out->header.nlen);
  191.     
  192.     MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
  193.  
  194.     if (out->bytes) {
  195.         if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
  196.             if (err=-43) {
  197.                 Create( out->name, out->vrefnum, '????','TEXT');
  198.                 if (FSOpen( out->name, out->vrefnum, &out->fd)) 
  199.                     return( 0L);
  200.                 }
  201.             else {
  202.                 return(0L);
  203.                 }
  204.             }
  205.         }
  206.     else {
  207.         if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
  208.             if (err=-43) {
  209.                 Create( out->name, out->vrefnum, '????','TEXT');
  210.                 if (OpenRF( out->name, out->vrefnum, &out->fd)) 
  211.                     return( 0L);
  212.                 }
  213.             else {
  214.                 return(0L);
  215.                 }
  216.             }
  217.         out->fork = 1;
  218.         out->bytes=BLOCKS(out->rlen)*128;
  219.         }
  220. }
  221.  
  222. MBread( in, buffer, size)
  223. MBFile *in;
  224. int size;
  225. char *buffer;
  226. {
  227.     char *p,*nptr;
  228.     int rsize=size;
  229.  
  230.  
  231.     if (in->fork<0) {
  232.         return(-1);
  233.         }
  234.  
  235.     p=buffer;
  236.     if (in->sector1) {
  237.         FileParam finfo;
  238.  
  239.         setmem( &in->header, sizeof(MBHead), 0);
  240.         PtoCstr(in->name);
  241.         nptr = rindex(in->name,':');
  242.         if(nptr != NULL){
  243.             nptr++;
  244.             movmem(  nptr, &in->header.nlen, strlen(nptr)+1);
  245.         } else {
  246.             movmem(in->name,&in->header.nlen,strlen(nptr)+1);
  247.         }
  248.         CtoPstr(in->name);
  249.         CtoPstr(&in->header.nlen);
  250.         GetFileInfo( in->vrefnum, in->name, &finfo);
  251.         movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
  252.         in->header.protected = (in->header.zero2 & 0x40)?1:0;
  253.         in->header.zero2 = 0;
  254.         lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
  255.         lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
  256.         lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
  257.         lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
  258.         in->dlen=finfo.ioFlLgLen;
  259.         in->rlen=finfo.ioFlRLgLen;
  260.         if (! (in->mode & MB_DISABLE) ) {
  261.             if (size<128) return(-1);
  262.  
  263.             movmem( &in->header, p, 128);
  264.             p +=128;
  265.             size -= 128;
  266.             in->bytes= BLOCKS(in->dlen)*128;
  267.             in->binary=1;
  268.         }
  269.         else {
  270.             in->bytes = in->dlen;
  271.             in->rlen=0;
  272.             in->binary=0;
  273.         }
  274.         in->sector1=0;
  275.         MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
  276.     }
  277.  
  278.     if ( size >0) {
  279.         long length = (long)size;
  280.         int err;
  281.  
  282.         err = FSRead( in->fd, &length, p);
  283.  
  284.         size -=(int)length;
  285.         in->bytes -=length;
  286.         p += length;
  287.  
  288.         if (err == -39 || (in->bytes<=0) ) {
  289.             if (in->bytes<0L) in->bytes=0L;
  290.             size -= (int)in->bytes;
  291.             p    +=      in->bytes;                /* Make adjustments for necessary 128 byte term */
  292.             if (!in->fork ) {
  293.                 in->fork=1;
  294.                 in->bytes= BLOCKS(in->rlen)*128;
  295.                 FSClose( in->fd);
  296.                 if (in->bytes) {
  297.                     OpenRF( in->name, in->vrefnum, &in->fd);
  298.                     length=(long)size;
  299.                     if (length >0L) {
  300.                         err = FSRead( in->fd, &length, p);
  301.                         size -= (int)length;
  302.                         in->bytes -=length;
  303.                         }
  304.                     }
  305.                 else {
  306.                     in->fd=0;
  307.                     in->fork=-1;                    /* Time to close up shop */
  308.                     }
  309.                 }
  310.             else {
  311.                 in->fd=0;
  312.                 in->fork=-1;                    /* Time to close up shop */
  313.                 }
  314.             }
  315.         }
  316.     return( rsize-size); 
  317. }
  318.  
  319. isMacBinary(p)
  320. MBHead *p;
  321. {
  322.     return( (p->zero1 == 0) &&
  323.             (p->zero2 == 0) &&
  324.             (p->zero3 == 0));
  325. }
  326.  
  327. MBstat()
  328. {
  329.     return(0);
  330. }
  331.